package com.lzq.chatroom_project.service.impl;

import cn.hutool.core.lang.hash.Hash;
import com.lzq.chatroom_project.common.utils.JacksonUtil;
import com.lzq.chatroom_project.dao.SensitiveWordMapper;
import com.lzq.chatroom_project.model.SensitiveWord;
import com.lzq.chatroom_project.service.SensitiveWordService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {
    private static final Logger log = LoggerFactory.getLogger(SensitiveWordServiceImpl.class);
    @Autowired
    private SensitiveWordMapper sensitiveWordMapper;

    private  List<String> sensitiveWords = new CopyOnWriteArrayList<>();
    private final HashSet<String> hashSet = new HashSet<>();
    private final Set<String> sensitiveWordsRemoveBufferSet = Collections.synchronizedSet(hashSet);


    @Override
    public void loadSensitiveWords() {
        List<String> sensitiveWordsFromDB = sensitiveWordMapper.selectAllSensitiveWords();
        log.info("敏感词列表已加载: " + JacksonUtil.writeValueAsString(sensitiveWordsFromDB));
        sensitiveWords.clear();
        sensitiveWords.addAll(sensitiveWordsFromDB);
    }

    @Override
    public List<String> getSensitiveWords() {
        return sensitiveWords;
    }

    @Override
    public Set<String> getsensitiveWordsRemoveBufferSet() {
        return sensitiveWordsRemoveBufferSet;
    }

    @Override
    public void addSensitiveWord(String word) {
        if (sensitiveWordsRemoveBufferSet.contains(word)) {
            sensitiveWordsRemoveBufferSet.remove(word);
            sensitiveWordMapper.insertExistedSensitiveWord(word);
            return;
        }
        SensitiveWord sensitiveWord = sensitiveWordMapper.findSensitiveWord(word);
        if (sensitiveWord == null) {
            sensitiveWordMapper.insertSensitiveWord(word);
        }else if (sensitiveWord.getIsDelete() == 0) {
            log.info("敏感词已存在, 不能重复添加!");
        } else if (sensitiveWord.getIsDelete() == 1) {
            sensitiveWordMapper.insertExistedSensitiveWord(word);
        } else {
            log.info("敏感词添加异常!");
        }
    }

    @Override
    public void removeSensitiveWord(String word) {
        SensitiveWord  sensitiveWord = sensitiveWordMapper.findSensitiveWord(word);
        if (sensitiveWord == null) {
            log.info("敏感词不存在, 不能删除!");
        }else if (sensitiveWord.getIsDelete() == 0) {
            sensitiveWordsRemoveBufferSet.add(word);
            sensitiveWordMapper.removeSensitiveWord(word);
        } else if (sensitiveWord.getIsDelete() == 1) {
            log.info("敏感词已删除, 不能重复删除!");
        } else {
            log.info("敏感词添加异常!");
        }
    }

    @Override
    public void removeSensitiveWordFromList(String removedSensitiveWord) {
        sensitiveWords = sensitiveWords.stream()
                .filter(item -> !item.contains(removedSensitiveWord)) // 过滤掉包含指定字符串的元素
                .collect(Collectors.toList());
        sensitiveWordsRemoveBufferSet.remove(removedSensitiveWord);
    }
}
